﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;

namespace DataAccess
{
   public class DataService
    {
       string chuoiKetnoi = ConfigurationManager.ConnectionStrings["Phong tro"].ConnectionString;
       SqlConnection _cn;
       void MoKetNoi()
       {
           _cn = new SqlConnection(chuoiKetnoi);
           _cn.Open();
       }
       void DongKetNoi()
       {
           _cn.Close();
       }
       public DataTable LayDuLieu(string sql, params SqlParameter[] thamso)
       {
           MoKetNoi();
           DataTable bangtrave = new DataTable();
           SqlCommand _sqlcm = new SqlCommand(sql, _cn);
           _sqlcm.Parameters.AddRange(thamso);
           SqlDataReader reader = _sqlcm.ExecuteReader();
           bangtrave.Load(reader);
           DongKetNoi();
           return bangtrave;
       }
       public bool CapNhatDuLieu(string sql, params SqlParameter[] thamso)
       {
           bool fag = false;
           MoKetNoi();
           SqlCommand _sqlcm = new SqlCommand(sql, _cn);
           _sqlcm.Parameters.AddRange(thamso);
           try
           {
               _sqlcm.ExecuteNonQuery();
               fag = true;
           }
           catch
           {
               fag = false;
           }
           DongKetNoi();
           return fag;
       }

    }
}
